#include "keyled.h"
#include "delay.h" 


//KEY Part
uint8_t mainKey = 0;
uint8_t mainKeyBuff = 0;

/**
 *@brief:按键初始化函数
 */
void Key_Init(void)
{
    
    GPIO_InitTypeDef  GPIO_InitStructure;	      
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);	//使能GPIOB时钟
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //GPIOB8 GPIOB9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;   //上拉输入
	GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
    
//    //中断级按键
//    EXTI_InitTypeDef   EXTI_InitStructure;
//    NVIC_InitTypeDef   NVIC_InitStructure;

//    /* Enable GPIOA clock */
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

//    /* Configure PD.02 pin as input pull up */
//    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//    GPIO_Init(GPIOD, &GPIO_InitStructure);

//    /* Enable AFIO clock */
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

//    /* Connect EXTI0 Line to PD.02 pin */
//    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2);

//    /* Configure EXTI2 line */
//    EXTI_InitStructure.EXTI_Line = EXTI_Line2;
//    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
//    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
//    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
//    EXTI_Init(&EXTI_InitStructure);

//    /* Enable and set EXTI2 Interrupt to the lowest priority */
//    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);
 
} 

//----------------------------------------------------------
//按键处理函数
//参数表：mode:0 不支持连续按，1 支持连续按
//返回值：0 没有任何按键按下，1 KEY1按下，2 KEY2按下，4 WKUP按下 WK_UP
//响应优先级,KEY0>KEY1>WK_UP
//----------------------------------------------------------
uint8_t Key_Scan(uint8_t mode)
{	 
	static uint8_t key_up=1; //按键按松开标志
	if(mode)key_up=1;        //支持连按		  
	if(key_up&&(KEY1_PIN==0||KEY2_PIN==0))
	{
		Delay_ms(10);//去抖动 
		key_up=0;
		if     (KEY1_PIN==0)  return KEY1;
		else if(KEY2_PIN==0)  return KEY2;
	}else if(KEY1_PIN==1&&KEY2_PIN==1)key_up=1; 	    
 	return 0;// 无按键按下
}


void InterruptKeyUpdate(void){
    
    static uint8_t lastKey = 0;
    uint8_t curKey = 0;

    if     (KEY1_PIN==0) curKey = KEY1;
    else if(KEY2_PIN==0) curKey = KEY2;
    
    if(lastKey == curKey){
//        if(curKey != mainKey){
//            lastMainKey = mainKey;
//        }
        if(curKey!=0){
            mainKeyBuff = curKey;
        }
        mainKey = curKey;
    }
    
    lastKey = curKey;
}


//LED Part


/**
 *@brief:LED灯初始化函数
 */
void LED_Init(void)  
{
    GPIO_InitTypeDef  GPIO_InitStructure;

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB ,ENABLE);	//使能GPIOB时钟
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   //推挽输出
	GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
   
    GPIO_SetBits(GPIOB,GPIO_Pin_10 | GPIO_Pin_11 );            //设置高，灯灭
}




















